package valid

import (
	"regexp"
	"strings"
)

func IsSpace(d string) bool {
	r, _ := regexp.Compile(`^[\s　]|[ ]$`)
	return r.MatchString(d)
}

func FormatTrimSpace(d string) string {
	return strings.TrimSpace(d)
}

func IsEnter(d string) bool {
	r, _ := regexp.Compile(`\n+`)
	return r.MatchString(d)
}

func FormatEnter(d string) string {
	return strings.Replace(d, "\n", "", -1)
}

func Pipe(formatList ...func(d string) (string, bool)) func(d string) string {

	return func(_d string) string {
		for _, format := range formatList {
			_d, _ = format(_d)
		}
		return _d
	}

}

func Not(check func(d string) bool) func(d string) bool {
	return func(d string) bool {
		return !check(d)
	}
}

func IsCn(d string) bool {
	r := regexp.MustCompile("^[\u4e00-\u9fa5]+$")
	return r.MatchString(d)
}

func FormatEmpty(d string) string {
	return d
}

func IsURL(d string) bool {
	r := regexp.MustCompile(`(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`)
	return r.MatchString(d)
}

func IsURLRecordNo(d string) bool {
	r := regexp.MustCompile(`^(粤ICP备)([0-9]+)号$`)
	return r.MatchString(d)
}
